package com.order.service.impl;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.order.config.OrderConfig;
import com.order.resp.Item;
import com.order.resp.OrderDetail;
import com.order.resp.RepsVo;
import com.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.LinkedHashMap;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OrderConfig orderConfig;

    @Override
    @HystrixCommand(fallbackMethod = "getError")
    public OrderDetail createOrder() {
        OrderDetail orderDetail = new OrderDetail();

        orderDetail.setOrderId(String.valueOf(System.currentTimeMillis()));
        orderDetail.setOrderDesc("测试订单");

        String goodsId = "1001";

        String url = orderConfig.getUrl();    //"http://127.0.0.1:8090/goods/query/{id}"; //写死

        //根据 实例名 获取服务列表：eureka

        //取出是个list。

        //随机，一人一个轮询， 权重 从list 选一个地址调用
        String queryGoodSUrl = "http://GOODS-SERVICE//goods/query/{id}";


        RepsVo repsVo = restTemplate.getForObject(queryGoodSUrl,RepsVo.class,goodsId);


        if (!"0".equals(repsVo.getCode())){
            return null;
        }

        LinkedHashMap item = (LinkedHashMap) repsVo.getData();

        orderDetail.setGoodsId(item.get("goodsId").toString());
        orderDetail.setGoodsName(item.get("goodsImg").toString());
        orderDetail.setGoodsTitel(item.get("goodsTitle").toString());
        orderDetail.setGoodsPrice(item.get("goodsPrice").toString());

        return orderDetail;
    }


    @Override
    public OrderDetail createOrder1() {
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderDesc("测试订单");
        orderDetail.setOrderId(String.valueOf(System.currentTimeMillis()));

        String url = orderConfig.getUrl();    //"http://127.0.0.1:8090/goods/query/{id}"; //写死

        String goodsId = "1001";


        //根据 实例名 获取服务列表：eureka

        //取出是个list。

        //随机，一人一个轮询， 权重 从list 选一个地址调用
        String queryGoodSUrl = "http://GOODS-SERVICE//goods/query/{id}";
        RepsVo repsVo = restTemplate.getForObject(queryGoodSUrl,RepsVo.class,goodsId);

        if (!"0".equals(repsVo.getCode())){
            return null;
        }

        LinkedHashMap item = (LinkedHashMap) repsVo.getData();
        orderDetail.setGoodsName(item.get("goodsImg").toString());
        orderDetail.setGoodsId(item.get("goodsId").toString());
        orderDetail.setGoodsTitel(item.get("goodsTitle").toString());
        orderDetail.setGoodsPrice(item.get("goodsPrice").toString());

        return orderDetail;
    }




    /**
     * 服务降级
     * @return
     */
    public OrderDetail getError(){

        System.out.println("------服务降级----");

        OrderDetail orderDetail = new OrderDetail();

        return orderDetail;
    }

}
